package com.asiainfo.ncrm.fragment;

import com.asiainfo.ncrm.R;
import com.asiainfo.ncrm.base.MyWebChromeClient;
import com.asiainfo.ncrm.common.Constant;
import com.asiainfo.ncrm.loading.LoadingView;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.RenderPriority;

@SuppressLint({"JavascriptInterface", "ValidFragment"})
public class fragmentPageBase extends Fragment{

	
	 private WebView webview;
	 
	 private String loadUrl;
	 
	 private int fragFrame;
	 
	  private LoadingView mLoadingView;
	 
	 public fragmentPageBase(String url,int frag ){
		 loadUrl = url;
		 fragFrame= frag;
	 };
	 
	 @Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			return  inflater.inflate(fragFrame, container, false); 
		}
		 
	   
	 

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		   
		 setView(); 
		 setListener();
		super.onActivityCreated(savedInstanceState);
	}




	private void setListener() {
		// TODO Auto-generated method stub
		webview.setWebChromeClient(new MyWebChromeClient(getActivity()));
		webview.getSettings().setJavaScriptEnabled(true);
		webview.getSettings().setRenderPriority(RenderPriority.HIGH);
		// 设置 缓存模式
		webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
		// 开启 DOM storage API 功能
		webview.getSettings().setDomStorageEnabled(true);
		// 开启 database storage API 功能
		webview.getSettings().setDatabaseEnabled(true);
		// 设置数据库缓存路径
		String cacheDirPath = getActivity().getFilesDir().getAbsolutePath()
				+ Constant.APP_CACAHE_DIRNAME;
		Log.i(Constant.TAG, "cacheDirPath=" + cacheDirPath);
		// 设置 Application Caches 缓存目录
		webview.getSettings().setDatabasePath(cacheDirPath);
		webview.getSettings().setAppCachePath(cacheDirPath);
		// 开启 Application Caches 功能
		webview.getSettings().setAppCacheEnabled(true);

		webview.addJavascriptInterface(new JavaScriptInterface(), "demo");

		// Display mDisplay = getWindowManager().getDefaultDisplay();
		// int width = mDisplay.getWidth();
		// int scalePercent = width*75/1024;
		// 三星table 187比率正常
		webview.setInitialScale(100);
		webview.getSettings().setUseWideViewPort(true);
		webview.getSettings().setLoadWithOverviewMode(true);
//		webview.loadUrl(Constant.OUTURL);
		webview.setWebViewClient(new WebViewClient(){
			@Override 
	        public void onPageFinished(WebView view,String url) 
	        { 
//	            dialog.dismiss(); 
				mLoadingView.setVisibility(View.GONE);
				webview.setVisibility(View.VISIBLE);
	        } 
		});
//		webview.loadUrl(Constant.OUTURL);
		webview.loadUrl(loadUrl);

	}

	final class JavaScriptInterface {
		JavaScriptInterface() {
		}

	}




	private void setView() {
		// TODO Auto-generated method stub
		 webview=(WebView)getView().findViewById(R.id.webView1);
		   mLoadingView= (LoadingView) getView().findViewById(R.id.loadView);
	}
}
